package com.ruoyi.dict.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dict.service.IDictUsageCheckService;
import com.ruoyi.repair.mapper.TFaultReportMapper;
import com.ruoyi.asset.mapper.TAssetMapper;
import com.ruoyi.inventory.mapper.TWarehouseTransactionMapper;
import com.ruoyi.inventory.mapper.TSpPartMapper;

/**
 * 字典使用情况检查服务实现
 * 
 * @author ruoyi
 */
@Service
public class DictUsageCheckServiceImpl implements IDictUsageCheckService 
{
    @Autowired
    private TFaultReportMapper tFaultReportMapper;
    
    @Autowired
    private TAssetMapper tAssetMapper;
    
    @Autowired
    private TWarehouseTransactionMapper tWarehouseTransactionMapper;
    
    @Autowired
    private TSpPartMapper tSpPartMapper;

    @Override
    public boolean isFaultTypeInUse(String faultTypeCode) 
    {
        // 检查故障报修表中是否使用了该故障类型
        int count = tFaultReportMapper.countByFaultType(faultTypeCode);
        return count > 0;
    }

    @Override
    public boolean isReportTypeInUse(String reportTypeCode) 
    {
        // 检查故障报修表中是否使用了该报修类型
        int count = tFaultReportMapper.countByReportType(reportTypeCode);
        return count > 0;
    }

    @Override
    public boolean isUrgencyLevelInUse(String urgencyCode) 
    {
        // 检查故障报修表中是否使用了该紧急程度
        int count = tFaultReportMapper.countByUrgency(urgencyCode);
        return count > 0;
    }

    @Override
    public boolean isEquipmentTypeInUse(String equipmentTypeCode) 
    {
        // 检查资产表中是否使用了该设备类型
        int count = tAssetMapper.countByEquipmentType(equipmentTypeCode);
        return count > 0;
    }

    @Override
    public boolean isWarehouseInUse(String warehouseCode) 
    {
        // 检查交易表中是否使用了该备件仓库
        int count = tWarehouseTransactionMapper.countByWarehouse(warehouseCode);
        return count > 0;
    }

    @Override
    public String getFaultTypeUsageDescription(String faultTypeCode) 
    {
        int count = tFaultReportMapper.countByFaultType(faultTypeCode);
        if (count > 0) {
            return String.format("该故障类型已被 %d 条报修记录使用，无法删除", count);
        }
        return null;
    }

    @Override
    public String getReportTypeUsageDescription(String reportTypeCode) 
    {
        int count = tFaultReportMapper.countByReportType(reportTypeCode);
        if (count > 0) {
            return String.format("该报修类型已被 %d 条报修记录使用，无法删除", count);
        }
        return null;
    }

    @Override
    public String getUrgencyLevelUsageDescription(String urgencyCode) 
    {
        int count = tFaultReportMapper.countByUrgency(urgencyCode);
        if (count > 0) {
            return String.format("该紧急程度已被 %d 条报修记录使用，无法删除", count);
        }
        return null;
    }

    @Override
    public String getEquipmentTypeUsageDescription(String equipmentTypeCode) 
    {
        int count = tAssetMapper.countByEquipmentType(equipmentTypeCode);
        if (count > 0) {
            return String.format("该设备类型已被 %d 个资产使用，无法删除", count);
        }
        return null;
    }

    @Override
    public String getWarehouseUsageDescription(String warehouseCode) 
    {
        int count = tWarehouseTransactionMapper.countByWarehouse(warehouseCode);
        if (count > 0) {
            return String.format("该备件仓库已被 %d 条交易记录使用，无法删除", count);
        }
        return null;
    }
    
    @Override
    public boolean isPartTypeInUse(String partTypeCode) 
    {
        // 检查备件表中是否使用了该备件类型
        int count = tSpPartMapper.countByPartType(partTypeCode);
        return count > 0;
    }
    
    @Override
    public String getPartTypeUsageDescription(String partTypeCode) 
    {
        int count = tSpPartMapper.countByPartType(partTypeCode);
        if (count > 0) {
            return String.format("该备件类型已被 %d 个备件使用，无法删除", count);
        }
        return null;
    }
}
